# -*- coding:utf-8 -*-
import urllib
import re
import logging
import datetime
import time

def getxcidlist(theurl):
    try:
        thestr = urllib.urlopen(theurl).read()
    except Exception,e:
        logging.error(str(e))
        return []

    regex = "/song/showcollect/id/.*?\""
    xcidlist = []
    for xc in re.finditer(regex,thestr):
        xc = thestr[xc.start():xc.end()]
        tmpid = xc[xc.index("id")+3:-1]
        tmpid = int(tmpid)
        xcidlist.append(tmpid)
    return xcidlist

def getxcrecommendpage(pid):
    theurl = "http://www.xiami.com/collect/recommend/page/"+str(pid)
    return getxcidlist(theurl)

def getxiamicollectionhot():
    """
    get xiami collection of the first page
    http://www.xiami.com/collect
    """
    theurl1 = "http://www.xiami.com/collect"
    theurl2 = "http://www.xiami.com/collect/recommend"
    #theurl3 = "http://www.xiami.com/collect/orinew"
    xciddic = {}
    l1 = getxcidlist(theurl1)
    l2 = getxcidlist(theurl2)
    #l3 = getxcidlist(theurl3)
    for i in l1:
        xciddic[i] = 1
    for i in l2:
        xciddic[i] = 1
    #for i in l3:
    #    xciddic[i] = 1

    xcidlist = xciddic.keys()
    return xcidlist

def getxiamicollectionbykeyword(k):
    xcidlist = []
    i = 1
    while True:
        theurl = "http://www.xiami.com/collect/search/key/%s/page/%d"%(k,i)
        xcidtmplist = getxcidlist(theurl)
        if len(xcidtmplist) != 0:
            xcidlist.extend(xcidtmplist)
            i += 1
        else:
            break
    return xcidlist


def getxiamicollectioninfo(xcid):
    """
    http://www.xiami.com/song/showcollect/id/8612850
    some info are changing over time
    so the play:view:recommend:store is with another info
    """
    dic = {}
    theurl = "http://www.xiami.com/song/showcollect/id/"+str(xcid)
    try:
        thestr = urllib.urlopen(theurl).read()
    except Exception,e:
        logging.error(str(e))
        return None
    
    #the name of the xc
    nameregex = "info_collect_main(\s|\S)*?</h2>"
    namem = re.search(nameregex,thestr)
    tmpstr = thestr[namem.start():namem.end()]
    h2index = tmpstr.index("<h2>")
    dic['name'] = tmpstr[h2index+4:tmpstr.index("<",h2index+4)]

    cdinforegex = "<div class=\"cdinfo(\s|\S)*?</div>"
    cdinfosearch = re.search(cdinforegex,thestr)
    if cdinfosearch:
        cdinfostr = thestr[cdinfosearch.start():cdinfosearch.end()]
    else:
        logging.error("not find cd info for "+str(xcid))
        return dic
    

    #cdinfo part
    lilist = []
    liregex = "<li>(\s|\S)*?</li>"
    for limatch in re.finditer(liregex,cdinfostr):
        lilist.append(cdinfostr[limatch.start():limatch.end()])
    ##li0 maker user id
    tmpstr = lilist[0]
    bindex = tmpstr.index('/u/')+3
    tmpstr = tmpstr[bindex:tmpstr.index("\"",bindex)]
    uid = int(tmpstr)
    dic['uid'] = uid

    ##li1 song number
    digitregex = "\d+"
    #print lilist[1]
    licountstr = ""
    for listr in lilist:
        if listr.count("歌曲")!= 0:
            licountstr = listr
            break
    if licountstr != "":
        csearch = re.search(digitregex,licountstr)
        cstr = licountstr[csearch.start():csearch.end()]
        songcount = int(cstr)
        if not dic.has_key('count'):
            dic['count'] = []
            dic['count'].append({})
        dic['count'][0]['songcount'] = songcount
        dic['count'][0]['curtime'] = str(time.time())

    ##li2 tags
    litagstr = ""
    for listr in lilist:
        if listr.count("标签") != 0:
            litagstr = listr
            break
    if litagstr != "":
        tagregex = "k\">(\s|\S)*?</a>"
        tags = []
        for tagm in re.finditer(tagregex,litagstr):
            tag = litagstr[tagm.start()+3:tagm.end()-4]
            tag = tag.strip()
            tags.append(tag)
        dic['tags'] = tags

    ##li3 date
    # some have no tags
    #print lilist[3]
    lidatestr = ""
    for listr in lilist:
        if listr.count("时间") != 0:
            lidatestr = listr
            break
    if lidatestr != "":
        dateregex = "\d+?-\d+?-\d+?"
        datem = re.search(dateregex,lidatestr)
        datestr = lidatestr[datem.start():datem.end()]
        dic['date'] = datestr

    ##li4 count, with the crawler datetime
    lisstr = ""
    for listr in lilist:
        if listr.count("浏览") != 0:
            lisstr = listr
            break
    if lisstr != "":
        digitregex = "\d+"
        numbers = []
        for dm in re.finditer(digitregex,lisstr):
            tmpstr = lisstr[dm.start():dm.end()]
            numbers.append(int(tmpstr))
        dic['count'][0]['playcount'] = numbers[0]
        dic['count'][0]['viewcount'] = numbers[1]
        dic['count'][0]['recommendcount'] = numbers[2]
        dic['count'][0]['storecount'] = numbers[3]

    #intro full
    inforegex = "<div class=\"info_intro_full(\s|\S)*?</div>"
    infom = re.search(inforegex,thestr)
    if infom:
        infostr = thestr[infom.start():infom.end()]
        dic['info'] = infostr
    else:
        dic['info'] = ""

    #songs part
    songlist = []
    songregex = "totle_up(\s|\S)*?</li>"
    for sm in re.finditer(songregex,thestr):
        songlist.append(thestr[sm.start():sm.end()])
    
    songs = []
    for songstr in songlist:
        tmpdic = {}
        #process the track part
        ##trackid
        idm = re.search("id\">\d+",songstr)
        trackid = int(songstr[idm.start()+4:idm.end()])
        tmpdic['trackid'] = trackid
        ##trackxiamiid
        sidm = re.search("song/\d+",songstr)
        songid = int(songstr[sidm.start()+5:sidm.end()])
        tmpdic['songid'] = songid
        ##artistxiamiid
        try:
            aidm = re.search("tist/\d+",songstr)
            aid = int(songstr[aidm.start()+5:aidm.end()])
            tmpdic['artistid'] = aid
        except Exception,e:
            #print songstr
            logging.error(str(e))
        #process the quote part
        qm = re.search('<em(\s|\S)*?</em>',songstr)
        if qm is not None:
            quote = songstr[qm.start():qm.end()]
            tmpdic['desp'] = quote
        songs.append(tmpdic)
    
    dic['songs'] = songs


    return dic



if __name__ == "__main__":
    #hotlist = getxiamicollectionhot()
    #print hotlist
    #info = getxiamicollectioninfo(1262337)
    #info = getxiamicollectioninfo(7288771)
    #info = getxiamicollectioninfo(8646216)
    #print info
    #print info['name']
    #print info['count']
    print getxcidlist("http://www.xiami.com/song/701")
    """
    tmpf = open("tmpf.txt","w")
    print>>tmpf,urllib.urlopen("http://www.xiami.com/song/701").read()
    tmpf.close()
    """
